package com.houduan.controller;

import com.houduan.dao.ComputerDao;
import com.houduan.model.Computer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ComputerServlet extends HttpServlet {

    private ComputerDao computerDao;

    @Override
    public void init() throws ServletException {
        computerDao = new ComputerDao();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        if (action == null) {
            action = "list";
        }

        switch (action) {
            case "list":
                listComputers(request, response);
                break;
            case "edit":
                showEditForm(request, response);
                break;
            case "delete":
                deleteComputer(request, response);
                break;
            default:
                listComputers(request, response);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        switch (action) {
            case "insert":
                insertComputer(request, response);
                break;
            case "update":
                updateComputer(request, response);
                break;
            default:
                listComputers(request, response);
                break;
        }
    }

    private void listComputers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Computer> computerList = computerDao.getAllComputers();
        request.setAttribute("computers", computerList);
        request.getRequestDispatcher("computer_management.jsp").forward(request, response);
    }

    private void insertComputer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            String status = request.getParameter("status");
            double price = Double.parseDouble(request.getParameter("price"));
            String description = request.getParameter("description");

            Computer computer = new Computer(0, name, type, status, price, description);
            computerDao.addComputer(computer);

            response.sendRedirect("ComputerServlet?action=list");
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("computer_management.jsp?error=新增电脑失败");
        }
    }

    private void updateComputer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            String status = request.getParameter("status");
            double price = Double.parseDouble(request.getParameter("price"));
            String description = request.getParameter("description");

            Computer computer = new Computer(id, name, type, status, price, description);
            computerDao.updateComputer(computer);

            response.sendRedirect("ComputerServlet?action=list");
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("computer_management.jsp?error=更新电脑失败");
        }
    }

    private void deleteComputer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            computerDao.deleteComputer(id);
            response.sendRedirect("ComputerServlet?action=list");
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("computer_management.jsp?error=删除电脑失败");
        }
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            Computer existingComputer = computerDao.getComputerById(id);
            request.setAttribute("computer", existingComputer);
            request.getRequestDispatcher("edit_computer.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("computer_management.jsp?error=加载编辑表单失败");
        }
    }
}
